package cn.jh.common.pay.utils;

import cn.jh.common.core.cn.Cards;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author Yichuan
 * @version 1.0
 */
public class PayBarConst {

    public static final String BASE_URL = "http://apis.miaowpay.com";
    public static final String AGENT_NO = "12000159";
    public static final String AGENT_KEY = "36580420a717492fa57cf03a292387c1";
    public static final String CARD04 = "CHANPAYNEWCARD_04";
    public static final String CARD0101 = "CHANPAYNEWCARD_0101";
    public static final String CARD0102 = "CHANPAYNEWCARD_0102";

    // 畅捷通道各银行支持的不同通道
    public static final HashMultimap<String, String> bankCodes = HashMultimap.create();

    static {
        bankCodes.put("ICBC", CARD04);
        bankCodes.put("BOC", CARD04);
        bankCodes.put("CCB", CARD04);
        bankCodes.put("CEB", CARD04);
        bankCodes.put("BOB", CARD04);
        bankCodes.put("CMBC", CARD04);
        bankCodes.put("CGB", CARD04);
        bankCodes.put("CIB", CARD04);
        bankCodes.put("PAB", CARD04);
        bankCodes.put("BCOM", CARD04);
        bankCodes.put("ABC", CARD04);
        bankCodes.put("CITIC", CARD04);
        bankCodes.put("HXB", CARD04);
        bankCodes.put("CMB", CARD04);
        bankCodes.put("EGBANK", CARD04);
        bankCodes.put("CZB", CARD04);
        bankCodes.put("HSBC", CARD04);
        bankCodes.put("HKBEA", CARD04);
        bankCodes.put("NCB", CARD04);
        bankCodes.put("HSB", CARD04);
        bankCodes.put("NJCB", CARD04);
        bankCodes.put("BJRCB", CARD04);
        bankCodes.put("ABC", CARD0101);
        bankCodes.put("BOC", CARD0101);
        bankCodes.put("CCB", CARD0101);
        bankCodes.put("CITIC", CARD0101);
        bankCodes.put("CGB", CARD0101);
        bankCodes.put("CMBC", CARD0101);
        bankCodes.put("PAB", CARD0101);
        bankCodes.put("HXB", CARD0101);
        bankCodes.put("BOB", CARD0101);
        bankCodes.put("ICBC", CARD0101);
        bankCodes.put("SHBANK", CARD0101);
        bankCodes.put("CEB", CARD0102);
        bankCodes.put("PSBC", CARD0102);
        bankCodes.put("BCOM", CARD0102);
        bankCodes.put("CMB", CARD0102);
        bankCodes.put("CIB", CARD0102);
        bankCodes.put("SPDB", CARD0102);
    }

    // 商户注册接口
    public static final String REGISTER_MERCHANT = "/rest/v1.0/paybar/registMerchant";

    // 商户开通产品接口
    public static final String REGISTER_MERCHANT_PRODUCT = "/rest/v1.0/paybar/registMerchantProduct";

    // 商户查询结算卡信息接口
    public static final String QUERY_MERCHANT_SETTLE = "/rest/v1.0/paybar/queryMerchantSettlementInfo";

    // 商户查询费率信息
    public static final String QUERY_MERCHANT_FEE = "/rest/v1.0/paybar/queryMerchantFeeInfo";

    // 商户修改结算卡信息接口
    public static final String MODIFY_MERCHANT_SETTLE = "/rest/v1.0/paybar/modifyMerchantSettlementInfo";

    // 商户修改产品费率接口
    public static final String MODIFY_MERCHANT_FEE = "/rest/v1.0/paybar/modifyMerchantFeeInfo";

    // 商户绑卡
    public static final String BIND_CARD = "/rest/v1.0/paybar/bindCard";

    // 商户绑卡短信确认
    public static final String CONFIRM_BIND_CARD = "/rest/v1.0/paybar/confirmBindCard";

    // 商户查询绑卡
    public static final String QUERY_BIND_CARD = "/rest/v1.0/paybar/queryBindCard";

    // 商户绑卡已绑卡
    public static final String QUERY_BIND_CARDS =  "/rest/v1.0/paybar/queryBankCardList";

    // 支付交易收单
    public static final String PAY = "/rest/v1.0/paybar/pay";

    // 快捷支付收单短信确认 -- 有短验支付才需要调用
    public static final String CONFIRM_PAY = "/rest/v1.0/paybar/confirmPay";

    // 支付交易查询
    public static final String QUERY_PAY = "/rest/v1.0/paybar/queryPay";

    // 代付提现接口
    public static final String WITHDRAW_DEPOSIT = "/rest/v1.0/paybar/withdrawDeposit";


    // 代付提现查询接口
    public static final String QUERY_WITHDRAW = "/rest/v1.0/paybar/queryWithdraw";

    // 商户余额查询
    public static final String MERCHANT_WALLET = "/rest/v1.0/paybar/queryMerchantWallet";

    /**
     * 获取订单号
     *
     * @param type 类型码
     * @return 返回订单号
     */
    public static String getOrderIdByUUId(int type) {
        Date date=new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        String time = format.format(date);
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        return time + (type < 10 ? ("0" + type) : type) + String.format("%011d", hashCodeV);
    }

    /**
     * 获取银行卡号对应可用的畅捷通道码
     *
     * @param cardNo 卡号
     * @return 返回可用的畅捷通道码集合
     */
    public static List<String> enabledChannel(String cardNo) {
        cn.jh.common.core.cn.CardInfo cardInfo = Cards.find(cardNo);
        if (cardInfo == null) {
            return ImmutableList.of();
        }
        Set<String> codes = bankCodes.get(cardInfo.getBankCode());
        if (codes == null || codes.isEmpty()) {
            return ImmutableList.of();
        }
        List<String> channelList = new ArrayList<>();
        ChannelCode channelCode;
        for (String code: codes) {
            channelCode = ChannelCode.code(code);

            if (channelCode == null) continue;

            channelList.add(channelCode.getValue());
        }
        return channelList;
    }
}
